/*
 *  Description: BOAT XU SOFT FHSS
 *  Version: V1.00
 *  Author: boat xu
 *  Email: xifengzui@outlook.com
 *  Date: 2020-07-14 08:50:39
 *  LastEditTime: 2020-07-18 16:21:15
 */ 
#include "bsf_types.h"
#include "bsf_itypes.h"
#include "bsf.h"
#include "bsf_timer.h"
#include "bsf_debug.h"
bsf_timestamp ts;
/**
 *  Description: those function use to operator the timer and it's funciton
 *  Param: 
 *  Return: 
 */
void bsf_timer_resetTimestamp(bsf_timestampTypes typeOftimestamp)
{
    
    ts.realTimeStamp = GetCurrentTimestamp();
    
    if((typeOftimestamp & b_tt_fhss) == b_tt_fhss)
    {
        ts.freqHoppTimeStamp  = ts.realTimeStamp;
        //dprintf("        Timestamp freqHoppTimeStamp Reset To:[%d]",ts.freqHoppTimeStamp);
    }

    if((typeOftimestamp & b_tt_abnormal) == b_tt_abnormal)
    {
        ts.abnormalTimeStamp  = ts.realTimeStamp;
        //dprintf("        Timestamp abnormalTimeStamp Reset To:[%d]",ts.abnormalTimeStamp);
    }

    if((typeOftimestamp & b_tt_ackTimeout) == b_tt_ackTimeout)
    {
        ts.ackTimeout  = ts.realTimeStamp;
        //dprintf("        Timestamp ackTimeout Reset To:[%d]",ts.ackTimeout);
    } 

    if((typeOftimestamp & b_tt_tabTimeout) == b_tt_tabTimeout)
    {
        ts.tabTimeStamp = ts.realTimeStamp;
        //dprintf("        Timestamp tabTimeStamp Reset To:[%d]",ts.tabTimeStamp);
    }
}

/**
 *  Description: 
 *  Param: 
 *  Return: 
 */
b_boolean bsf_timer_isSelectTimestampGreateThan(bsf_timestampTypes selectTimestamp,b_u16_t val)
{
    b_u16_t curTimeStamp = GetCurrentTimestamp();
    b_u16_t error = 0;
    b_boolean  ret = b_false;
    if((selectTimestamp & b_tt_fhss) == b_tt_fhss)
    {
        if(curTimeStamp < ts.freqHoppTimeStamp)
        {
            error = 65535 - ts.freqHoppTimeStamp;
            error += curTimeStamp;
        }
        else
        {
            error = curTimeStamp - ts.freqHoppTimeStamp;
        }
        
        if(error > val)
        {
            ret =  b_true;
        }
        //dprintf("        Timestamp freqHoppTimeStamp error:%d",error);
    }

    if((selectTimestamp & b_tt_abnormal) == b_tt_abnormal)
    {
        if(curTimeStamp < ts.abnormalTimeStamp)
        {
            error = 65535 - ts.abnormalTimeStamp;
            error += curTimeStamp;
        }
        else
        {
            error = curTimeStamp - ts.abnormalTimeStamp;
        }
        
        if(error > val)
        {
            ret =  b_true;
        }
        //dprintf("        Timestamp abnormalTimeStamp error:%d",error);
    }
    
    if((selectTimestamp & b_tt_ackTimeout) == b_tt_ackTimeout)
    {
        if(curTimeStamp < ts.ackTimeout)
        {
            error = 65535 - ts.ackTimeout;
            error += curTimeStamp;
        }
        else
        {
            error = curTimeStamp - ts.ackTimeout;
        }
        
        if(error > val)
        {
            ret =  b_true;
        }
        //dprintf("        Timestamp ackTimeout error:%d",error);
    }
    if((selectTimestamp & b_tt_tabTimeout) == b_tt_tabTimeout)
    {
        if(curTimeStamp < ts.tabTimeStamp)
        {
            error = 65535 - ts.tabTimeStamp;
            error += curTimeStamp;
        }
        else
        {
            error = curTimeStamp - ts.tabTimeStamp;
        }
        
        if(error > val)
        {
            ret =  b_true;
        }
        //dprintf("        Timestamp tabTimeStamp error:[%d]",error);
    }
    //dprintf("        Current Count:[%d]",curTimeStamp);
    return ret;
}
